﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CCOMDataAccess;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void wEngagement_ActiveStepChanged(object sender, EventArgs e)
    {
        if (wEngagement.ActiveStep.Name == "Recapitulatif")
        {
            lBirthDate.Text = tbDay.Text + '/' + tbMonth.Text + '/' + tbYear.Text;
            lName.Text = tbName.Text;
            lPrename.Text = tbPrename.Text;
            lPseudo.Text = tbPseudo.Text;
            lPassword.Text = tbPassword.Text;
            lMail.Text = tbMail.Text;
            lPhoneNumber.Text = tbPhoneNumber.Text;
            lEquipment.Text = tbEquipement.Text;
            if (rbRadio.SelectedValue == "1")
                lRadio.Text = "Oui";
            else
                lRadio.Text = "Non";
        }
    }

    protected void wEngagement_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
    DataClassesDataContext db = new DataClassesDataContext();

        /* User object creation */
        User user = new User();
        user.Rank = int.Parse("4");
        user.EngagementDate = DateTime.Now;
        DateTime dtBirth = new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text));
        user.BirthDate = dtBirth;
        user.Equipment = lEquipment.Text;
        user.Experience = 0;
        user.FirePower = 0;
        user.Mail = lMail.Text;
        user.PhoneNumber = lPhoneNumber.Text;
        user.Name = lName.Text;
        user.Prename = lPrename.Text;
        user.Pseudo = lPseudo.Text;
        user.Password = lPassword.Text;
        user.Radio = Int32.Parse(rbRadio.SelectedValue);
        db.Users.InsertOnSubmit(user);
        db.SubmitChanges();

        /* Assigning new user to the recrutment squad */
        User newUser = (User)from u in db.Users
                    where u.Pseudo == lPseudo.Text
                    select u;
        UserToSquad usRecrut = new UserToSquad();
        usRecrut.Squad = 53;
        usRecrut.User = newUser.Matricule;
        db.UserToSquads.InsertOnSubmit(usRecrut);
        db.SubmitChanges();

        UserManager.Authenticate(user.Pseudo, user.Password);
        Response.Redirect("/CCOMWebsite/RegistrationFinal.aspx");
    }
}
